/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.ugent.medirem.persistence.facade;

import be.ugent.medirem.persistence.entity.MeasurementType;
import be.ugent.medirem.persistence.entity.Unit;
import be.ugent.medirem.persistence.entity.details.MeasurementTypeDetails;
import be.ugent.medirem.persistence.entity.values.MeasurementTypeValues;
import be.ugent.medirem.persistence.exception.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Thomas roelens
 */
@Stateless
public class MeasurementTypeDaoBean implements MeasurementTypeDaoLocal {

    @PersistenceContext
    private EntityManager em;

    public Long create(MeasurementTypeValues details) throws IllegalArgumentException {
        MeasurementType type = null;
        Unit unit = null;

        try {
            unit = em.find(Unit.class, details.getUnitId());
            type = new MeasurementType(details.getName(), unit);
            em.persist(type);
        } catch (Exception ex) {
            throw new EJBException(ex);
        }

        return type.getId();
    }

    public void remove(Long id) throws IllegalArgumentException, EntityNotFoundException {
        MeasurementType type = null;

        if (id == null) {
            throw new IllegalArgumentException("MeasurementType id is null");
        }

        try {
            type = em.find(MeasurementType.class, id);
            if (type == null) {
                throw new EntityNotFoundException();
            } else {
                type = em.merge(type);
                em.remove(type);
            }
        } catch (Exception ex) {
            throw new EJBException(ex);
        }
    }

    public MeasurementTypeDetails getMeasurementType(Long id) throws IllegalArgumentException {
        MeasurementType type = null;
        MeasurementTypeDetails details = null;

        if (id == null) {
            throw new IllegalArgumentException("MeasurementType id is null");
        }

        try {
            type = em.find(MeasurementType.class, id);
            if (type == null) {
                throw new EntityNotFoundException();
            } else {
                details = new MeasurementTypeDetails(type.getId(), type.getName(), type.getUnit().getName());
            }
        } catch (Exception ex) {
            throw new EJBException(ex);
        }
        return details;
    }

    public List<MeasurementTypeDetails> getAll() {
        List<MeasurementTypeDetails> list = new ArrayList<MeasurementTypeDetails>();

        List<MeasurementType> measurementTypeList = em.createNamedQuery("MeasurementType.FindAll").getResultList();
        for (MeasurementType u : measurementTypeList) {
            list.add(new MeasurementTypeDetails(u.getId(), u.getName(), u.getUnit().getName()));
        }

        return list;
    }

    public Long getMeasurementTypeId(String name) throws IllegalArgumentException, EntityNotFoundException {
        MeasurementType type = null;
        try {
            type = (MeasurementType) em.createNamedQuery("MeasurementType.FindByName").setParameter("name", name).getResultList().get(0);
            if (type == null) {
                throw new EntityNotFoundException();
            }
        } catch (Exception ex) {
            throw new IllegalArgumentException("MeasurmentType name invalid");
        }
        return type.getId();
    }

    public void persist(Object object) {
        em.persist(object);
    }
}
